//player.cpp
#include "player.h"
#include "networking.h"

Player::Player(){
  playerName="Player";
  bank = 1000;
  playerIndex = 0;
}

Player::Player(string name, int size, int initBank){
  playerName = name;
  handSize = size;
  bank = initBank;
  playerIndex = 0;
}

void Player::DealCard(Deck& thisDeck){
  hand[playerIndex]=thisDeck.DrawCard();
  playerIndex++;
}

void Player::PrintHand(){
  cout<<playerName<<"'s hand:"<<endl;
  int i;
  for(i=0;i<playerIndex;i++)
    {
      cout<<i+1<<": ";
      cout<<hand[i]->StringCard()<<endl;
    }
}

void Player::PrintHandForClient(){
  char* reply;
  network gameServer;
  reply = gameServer.runServer("Your_hand_");
  int i;
  for(i=0; i< playerIndex; i++)
    {
      //cout<<endl;
      //cout<<"in print hand for client loop"<<endl;
      cout<<""<<endl;
      string temp;
      temp = hand[i]->StringCard();
      const char* logitech = temp.c_str();
      //cout<<"logitech: "<<logitech<<endl;
      cout<<""<<endl;
      reply = gameServer.runServer(logitech);
    }
}

void Player::SetName(string name){
  playerName=name;
}

void Player::ResetHand(){
  playerIndex=0;
}

int Player::BlackjackValue(){
  int cardValue=0;
  int bjValue=0;
  int k;
  for(k=0;k<playerIndex;k++)
    {
      cardValue=hand[k]->GetValue();
      if((cardValue!=1)&&(cardValue<11))
	bjValue+=cardValue;
      else if((cardValue==1)&&(bjValue<11))
	bjValue+=11;
      else if(cardValue>10)
	bjValue+=10;
      else
	bjValue+=1;
    }
  return bjValue;
}

void Player::PlaceWager(int amt){
  int newAmt;
  wager = amt;
  if(wager<bank+1)
    bank=bank-wager;
  else
    {
      cout<<"Place smaller bet."<<endl<<"Current bank: "<<bank<<endl;
      cout<<"New wager: ";
      cin>>newAmt;
      PlaceWager(newAmt);
    }
}

bool Player::DoubleDown(){
  if(bank-wager>=0){
    bank = bank-wager;
    wager = wager * 2;
    return true;
  }
  return false;
}


void Player::GetWinnings(int win){
  bank+=win;
  wager=0;
  cout<<playerName<<" new bank: "<<bank<<endl;
}

void Player::LostHand(){
  wager=0;
  cout<<playerName<<" new bank: "<<bank<<endl;

}

int Player::GetWager(){
  return wager;
}

const char* Player::GetName(){
  return playerName.c_str();
}
